package org.ainlolcat.ainscience.column.threshold;

import org.ainlolcat.Variable;
import org.ainlolcat.aincalc2.AinCalc;
import org.ainlolcat.aincalc2.Cleks;
import org.ainlolcat.aincalc2.Leksemma;
import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.utils.executor.Executor;
import org.ainlolcat.ainscience.utils.executor.ExecutorProvider;

/**
* @author ainlolcat
*         Date: 5/5/13
*/
public abstract class AbstractThreshold implements Threshold{
    int type;
    double value;
    boolean filter = false;
    boolean localFilter = false;
    String formula;

    public AbstractThreshold(int type, double value, boolean filter, boolean localFilter) {
        this.type = type;
        this.value = value;
        this.filter = filter;
        this.localFilter = localFilter;
    }

    public int getType() {
        return type;
    }

    public boolean isFilter() {
        return filter;
    }

    public boolean isLocalFilter() {
        return localFilter;
    }

    protected Threshold setFormula(String formula){
        this.formula = formula;
        return this;
    }

    protected Variable getValue(Entity entity, int column, int row) {
        if (formula == null)
            return entity.getColumnByOrder(column).getCanonicalValue(row);
        else {
            Executor executor = ExecutorProvider.getExecutor(entity);
            try{
                Object value =  executor.execute(formula).getStringValue();
                return new Variable(value, Variable.TYPE_DOUBLE);
            } catch (Exception e) {
                throw new RuntimeException("Cannot handle formula: " + formula + " on row " + row, e);
            }
        }
    }
}
